package com.example.myjavafx.model;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.StringProperty;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class UserDto {
    public int id;
    public String user_name;
    public String user_password;
    public int age;

    // Getters and setters
    // ...

    public UserDto(int id, String user_name, String user_password, int age) {
        this.id = id;
        this.user_name = user_name;
        this.user_password = user_password;
        this.age = age;
    }

    public UserDto() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public String getUser_password() {
        return user_password;
    }

    public void setUser_password(String user_password) {
        this.user_password = user_password;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public static Users fromDto(UserDto dto) {
        return new Users(dto.getId(), dto.getUser_name(), dto.getUser_password(), dto.getAge());
    }

    public static UserDto toDto(Users user) {
        UserDto dto = new UserDto();
        dto.setId(user.getId());
        dto.setUser_name(user.getUser_name());
        dto.setUser_password(user.getUser_password());
        dto.setAge(user.getAge());
        return dto;
    }
    public static List<Users> parseJson(String json) {
        Gson gson = new Gson();
        Type userDtoListType = new TypeToken<List<UserDto>>(){}.getType();
        List<UserDto> userDtos = gson.fromJson(json, userDtoListType);

        List<Users> users = new ArrayList<>();
        for (UserDto dto : userDtos) {
            users.add(fromDto(dto));
        }
//        return List.of(
//                new User("张三", 25),
//                new User("李四", 30)
//        );
        return users;
    }
    public static List<Users> parseJsonToList(String json)  {
        ObjectMapper mapper = new ObjectMapper();
        List<UserDto> userDtos = null;
        try {
            userDtos = mapper.readValue(json, new TypeReference<List<UserDto>>() {
            });
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        List<Users> users = new ArrayList<>();
        for (UserDto dto : userDtos) {
            users.add(fromDto(dto));
        }
        return users;
//        List<MyObject> list = mapper.readValue(jsonString, new TypeReference<List<MyObject>>() {});

    }

}